<?php

Class Yapary_WWW_StreamContext
{
	CONST METHOD_POST		= 'POST';
	CONST METHOD_GET		= 'GET';

	CONST PROTOCOL_HTTP		= 'http';
	CONST PROTOCOL_HTTPS	= 'https';
	CONST PROTOCOL_FTP		= 'ftp';

	CONST CONTENT_TYPE_MULTIPART	= 'multipart/form-data';
	CONST CONTENT_TYPE_APPLICATION	= 'application/x-www-form-urlencoded';

	CONST CONNECTION_KEEP_ALIVE		= 'keep-alive';

	CONST CHARSET_UTF8				= 'utf-8';
	CONST CHARSET_ISO8859_1			= 'ISO-8859-1';

	private $method			= NULL;
	private $methodData		= array();
	private $contentType	= NULL;
	private $userAgent		= NULL;
	private $accept			= NULL;
	private $acceptLanguage	= NULL;
	private $acceptCharset	= NULL;
	private $acceptEncoding	= NULL;
	private $keepAlive		= NULL;
	private $connection		= NULL;
	private $dnt			= NULL;
	private $referer		= NULL;

	private $arrCookie		= NULL;


	function __construct () {
	}


	/**
	 * @param string $_method				: {POST|GET}
	 * @param array|string $_arrMethodData	: POST or GET data as array array(var1=>val1, var2=>val2, ...)
	 * */
	public function setMethod ($_method, $_arrOrStringData) {
		$this->method		= $_method;
		$this->methodData	= $_arrOrStringData;
	}


	/**
	 * @param string $_acceptLanguage	: {de|en|...}
	 * */
	public function setAcceptLanguage ($_acceptLanguage) {
		$this->acceptLanguage	= $_acceptLanguage;
	}


	/**
	 * @param string $_accept			: {application/json|text/javascript}
	 * */
	public function setAccept ($_accept) {
		$this->accept			= $_accept;
	}


	/**
	 * @param string $_acceptCharset	: {ISO-8859-1|utf-8}
	 * */
	public function setAcceptCharset ($_acceptCharset) {
		$this->acceptCharset	= $_acceptCharset;
	}


	/**
	 * @param string $_accept			: {application/json|text/javascript}
	 * */
	public function setAcceptEncoding ($_acceptEncoding) {
		$this->acceptEncoding	= $_acceptEncoding;
	}


	/**
	 * @param string $_contentType		: {application/x-www-form-urlencoded|multipart/form-data}
	 * */
	public function setContentType ($_contentType) {
		$this->contentType		= $_contentType;
	}


	/**
	 * @param string $_seconds			: {1-X}
	 * */
	public function setkeepAlive ($_seconds) {
		$this->keepAlive	= $_seconds;
	}


	/**
	 * @param string $_connection			: {keep-alive|}
	 * */
	public function setConnection ($_connection) {
		$this->connection	= $_connection;
	}


	/**
	 * @param string $_dnt			: {1-}
	 * */
	public function setDNT($_dnt) {
		$this->dnt			= $_dnt;
	}


	/**
	 * @param string $_url			: URI-String
	 * */
	public function setReferer($_url) {
		$this->referer			= $_url;
	}


	/**
	 * @param string|array $_string_or_array_Cookie	: Cookie data as string or array array(var1=>val1, var2=>val2, ...)
	 * */
	public function setCookie ($_string_or_array_Cookie) {
		$this->arrCookie	= $_string_or_array_Cookie;
	}


	/**
	 * @param string $_userAgeng	: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1
	 * */
	public function setUserAgent ($_userAgent) {
		$this->userAgent	= $_userAgent;
		ini_set('user_agent', $_userAgent);
	}


	public function getStreamHeader ($_protocol = 'http') {
		$arrHttp		= array();
		$arrHeader		= array();
		$content		= NULL;
		$header			= NULL;

		if (isset($this->accept)) {
			$header		.= "Accept: ".$this->accept."\r\n";
		}
		if (isset($this->acceptLanguage)) {
			$header		.= "Accept-language: ".$this->acceptLanguage."\r\n";
		}
		if (isset($this->acceptEncoding)) {
			$header		.= "Accept-Encoding: ".$this->acceptEncoding."\r\n";
		}
		if (isset($this->acceptCharset)) {
			$header		.= "Accept-Charset: ".$this->acceptCharset."\r\n";
		}
		if (isset($this->contentType)) {
			$header		.= "Content-type: ".$this->contentType."\r\n";
		}
		if (isset($this->dnt)) {
			$header		.= "DNT: ".$this->dnt."\r\n";
		}
		if (isset($this->keepAlive)) {
			$header		.= "Keep-Alive: ".$this->keepAlive."\r\n";
		}
		if (isset($this->referer)) {
			$header		.= "Referer: ".$this->referer."\r\n";
		}
		if (isset($this->connection)) {
			$header		.= "Connection: ".$this->connection."\r\n";
		}

		if ($this->arrCookie !== NULL) {
			if (is_array($this->arrCookie) && count($this->arrCookie)) {
				$header		.= "Cookie: ".http_build_query($this->arrCookie, '', '; ')."\r\n";
			} else {
				$header		.= "Cookie: ".$this->arrCookie."\r\n";
			}

		}

		if (isset($this->method)) {
			if (is_array($this->methodData)) {
				$content	= http_build_query($this->methodData);
			} else {
				$content	= $this->methodData;
			}
			$header		.= "Content-Length: ".strlen($content)."\r\n\r\n";
			$arrHttp['method']	= $this->method;
			$arrHttp['content']	= $content;
		}

		if ($this->userAgent !== NULL) {
			$arrHttp['User-Agent']	= $this->userAgent;
		}

		if ($header !== NULL) {
			$arrHttp['header']	= $header;
		}


		$options = array(
				$_protocol	=> $arrHttp);

		//echo '<pre>'.print_r($options, true).'</pre>';
		return $options;
	}


	public function getStreamContext () {
		return stream_context_create($this->getStreamHeader());
	}

}